/*
 * ttrss-reader-fork for Android
 * 
 * Copyright (C) 2010 N. Braden.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

package org.ttrssreader.widget;

import org.ttrssreader.R;
import org.ttrssreader.controllers.DBHelper;
import org.ttrssreader.model.pojos.Article;
import org.ttrssreader.utils.Utils;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

public class TickerWidgetService extends RemoteViewsService {
    
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new TickerRemoteViewsFactory(this.getApplicationContext(), intent);
    }
    
}

class TickerRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    private Article[] mWidgetItems; // = new ArrayList<Article>();
    private Context mContext;
    private int mAppWidgetId;

    public TickerRemoteViewsFactory(Context context, Intent intent) {
        mContext = context;
        mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    @Override
    public int getCount() {
        return mWidgetItems.length;
    }

    @Override
    public long getItemId(int position) {
        return mWidgetItems[position].feedId;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public RemoteViews getViewAt(int position) {
        RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.widget_ticker_item);
        DBHelper db = DBHelper.getInstance();
        views.setTextViewText(R.id.tvArticleTitle, mWidgetItems[position].title);
        views.setTextViewText(R.id.tvFeedTitle, db.getFeed(mWidgetItems[position].feedId).title);
        views.setTextViewText(R.id.tvDate, DateFormat.format("MMM d", mWidgetItems[position].updated));
        
        Bundle extras = new Bundle();
        extras.putInt(TickerWidget.ARTICLE_ID, mWidgetItems[position].id);
        extras.putInt(TickerWidget.FEED_ID, db.getFeed(mWidgetItems[position].feedId).id);
        Intent fillInIntent = new Intent();
        fillInIntent.putExtras(extras);
        views.setOnClickFillInIntent(R.id.widget_ticker_item, fillInIntent);
        return views;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
     // In onCreate() you setup any connections / cursors to your data source. Heavy lifting,
        // for example downloading or creating content etc, should be deferred to onDataSetChanged()
        // or getViewAt(). Taking more than 20 seconds in this call will result in an ANR.
        /*for (int i = 0; i < mCount; i++) {
            DBHelper db = DBHelper.getInstance();
            mWidgetItems = db.getUnreadArticles(-4).toArray(new Article[0]);
        }*/

    }

    @Override
    public void onDataSetChanged() {
        if(mWidgetItems != null) {
            return;
        }
        
        
        //AppWidgetManager awm = AppWidgetManager.getInstance(mContext);
        String itemSettings = WidgetConfigureActivity.loadIdPref(mContext, mAppWidgetId);
        int itemId;
        
        if(itemSettings != null) {
            Log.d(Utils.TAG, itemSettings);
            String[] settings = itemSettings.split("/");
            //TODO update to handle categories
            itemId = Integer.parseInt(settings[1]);
            mWidgetItems = DBHelper.getInstance().getUnreadArticles(itemId).toArray(new Article[0]);
        }
    }

    @Override
    public void onDestroy() {
        mWidgetItems = null;
        
    }
    
}